<?php

/**
 * Menu
 *
 * @author Administrator
 * @version
 */
class Model_Tables_Menu extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'menu';

    public function getMenuList( $menuid ,$filter="3,2,1,0")
    {
    	$sql = "select * from menu where `parentid`='$menuid' order by listorder,menuid";
    	$result = $this->_db->fetchAll($sql);
        return $result;
    	/*
        $orderby = array('listorder', 'menuid');
        $result = $this->fetchAll('`parentid`=' . $menuid, $orderby );
        return $result->toArray();*/
    }

    public function get($menuid,$fields = '*')
    {
        $menuid = intval($menuid);
        $row = $this->find($menuid)->toArray();
        if (empty($row)) {
            throw new Exception("Count not find row $menuid");
        }
        return $row[0];
    }

    public function getPartMenuList( $parentid )
    {
        $orderby = array('listorder', 'menuid');
        $result = $this->fetchAll('`parentid`=' . $parentid, $orderby );
        return $result->toArray();
    }

    public function getMenuCount( $parentid )
    {
    	$select = $this->select()
                ->from($this->_name,'COUNT(*) AS num')
                ->where('parentid=?', $parentid)
                ->order(array('listorder', 'menuid'));
        $result = $this->fetchRow($select)->num;
        return $result;
    }

    public function getPartMenuObj( $parentid, $page, $numPerPage )
    {
    	$page = (1>$page) ? 1 : $page;
    	$count = $numPerPage;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('listorder', 'menuid');
        $result = $this->fetchAll('`parentid`=' . $parentid,
                    $orderby, $count, $offset )->toArray();
        return $result;
    }

    public function saveMenu( $params )
    {
        $data = array(
                'parentid'=> $params['parentid'],
                'name'=> $params['name'],
                'url'=> $params['url'],
                'target'=> $params['target'],
//                'style'=> $params['style'],
//                'roleids'=> $params['roleids'],
                'keyid'=> $params['keyid'],
                'resourceid'=> $params['resourceid'],
//                'js'=> $params['js']);
                );
        return $this->insert($data);
    }

    public function updateMenu( $menuid, $params )
    {
        $data = array(
                'parentid'=> $params['parentid'],
                'name'=> $params['name'],
                'url'=> $params['url'],
                'target'=> $params['target'],
//                'style'=> $params['style'],
//                'roleids'=> $params['roleids'],
                'resourceid'=> $params['resourceid'],
//                'js'=> $params['js']);
                );
        $where = 'menuid = '.$menuid;
        $this->update($data , $where );
    }

    public function updateMenuName( $menuid, $menuname )
    {
        $data = array('name'=> $menuname);
        $where = 'menuid = '.$menuid;
        $this->update($data , $where );
        return true;
    }

    public function updateMenuOrder( $menuid, $menuorder )
    {

        $data = array('listorder'=> intval($menuorder));
        $where = 'menuid = '.$menuid;
        $this->update($data , $where );
        return true;
    }

    public function deleteMenu( $menuid )
    {
        $where = 'menuid = '.$menuid;
        $this->delete($where);
        return true;
    }

    public function listResource( )
    {
        $sql = 'select id, resourcename from resource';
        $result = $this->_db->fetchAll($sql);
        return $result;
    }

    public function getChild( $parentid ,$filter="3,2")
    {
    	$sql = "select * from menu where `parentid`='$parentid' and state in ($filter) order by listorder,menuid";
    	$result = $this->_db->fetchAll($sql);
        return $result;
    	/*    	
        $orderby = array('listorder', 'menuid');
        $result = $this->fetchAll('`parentid`=' . $parentid,
                    $orderby )->toArray();
        return $result;*/
    }

	public function getByKeyid($keyid)
	{
		$sql = "select * from menu where keyid='".$keyid."'";
		$result = $this->_db->query($sql)->fetchAll();
        return $result;
	}

	public function update_isfolder()
	{
		$menuids_r = $this->_db->query("SELECT distinct(parentid) FROM menu WHERE parentid>0")->fetchAll();
		$menuidarr = array();
		if (is_array($menuids_r) && !empty($menuids_r))
		{
			foreach ($menuids_r as $menuid)
			{
				$menuidarr[] = $menuid['parentid'];
			}
			$menuids = implode(',',$menuidarr);
            $this->_db->query("UPDATE `$this->_name` SET `isfolder`=1 WHERE `menuid` IN ($menuids)");
            $this->_db->query("UPDATE `$this->_name` SET `isfolder`=0 WHERE `menuid` NOT IN ($menuids)");
		}
		return ;
	}

	public function updateMenuFolderStatus( $menuid, $isFolder )
    {
        $data = array('isfolder'=> $isFolder);
        $where = 'menuid = '.$menuid;
        $this->update($data , $where );
        return true;
    }
}
